﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using GpxToolsLib;
using System.IO;
using System.Xml.Serialization;

namespace TrailMap.Utils
{
    public class GpxFiles
    {
        private static ObservableCollection<gpx> loadedGpxfiles = new ObservableCollection<gpx>();

        public static gpx LoadGpx()
        {
            OpenFileDialog opfd = new OpenFileDialog();

            opfd.Filter = "GPX Files|*.gpx";
            opfd.Multiselect = false;

            if (opfd.ShowDialog() == true)
            {
                GPXLoader loader = new GPXLoader();

                gpx gpxFile = loader.LoadGPXFile(opfd.File.OpenRead());

                loadedGpxfiles.Add(gpxFile);
                return gpxFile;
            }

            return null;
        }

        public static void SaveGpx(gpx gpxData)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter = "GPX Files|*.gpx";

            if (sfd.ShowDialog() == true)
            {

                using (Stream fs = (Stream)sfd.OpenFile())
                {
                    XmlSerializer ser = new XmlSerializer(typeof(gpx),@"http://www.topografix.com/GPX/1/1");

                    ser.Serialize(fs, gpxData);

                    fs.Close();
                }
            }
        }

    }
}
